package co.gov.medellin.ssti.sub.web.action;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.beans.factory.annotation.Autowired;

import co.gov.medellin.ssti.sub.common.excepcion.SUBException;
import co.gov.medellin.ssti.sub.model.dao.exception.DAOException;
import co.gov.medellin.ssti.sub.model.service.SubprogramaService;
import co.gov.medellin.ssti.sub.model.vo.SubprogramaVO;
import co.gov.medellin.ssti.sub.paoyer.administration.model.vo.UserVO;

public class SubModSubprogramaAction extends Action {
	@Autowired
	SubprogramaService subprogramaServiceImpl;

	/**
	 * This is the main action called from the Struts framework.
	 * 
	 * @param mapping
	 *            The ActionMapping used to select this instance.
	 * @param form
	 *            The optional ActionForm bean for this request.
	 * @param request
	 *            The HTTP Request we are processing.
	 * @param response
	 *            The HTTP Response we are processing.
	 * @return
	 * @throws java.io.IOException
	 * @throws javax.servlet.ServletException
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		// Proceso para la auditoria
		UserVO user = (UserVO) request.getSession().getAttribute("userInfo");
		String terminalAddress = request.getRemoteAddr();
		String strTerminal = ("WEB=" + terminalAddress);
		Calendar c = Calendar.getInstance();
		SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
		// Fin del proceso

		String strvalor11 = null;

		String strOption = "modsub";
		request.setAttribute("option", strOption);

		String subprog = request.getParameter("Subprograma");
		Object obsub = request.getParameter("Subprograma");
		if (obsub != null) {
			subprog = request.getParameter("Subprograma");
		} else {
			subprog = request.getParameter("Codigo");
		}

		//
		HttpSession session = request.getSession();

		SubprogramaVO subprogramaVO = new SubprogramaVO();
		subprogramaVO.setUsuarioModifica(user.getName());
		subprogramaVO.setFechaModifica(dateFormat.format(c.getTime()));
		subprogramaVO.setTerminalModifica(strTerminal);
		subprogramaVO.setCode(Long.valueOf(subprog));

		subprogramaVO.setNombre(request.getParameter("subprograma"));
		try {
			subprogramaVO.setCodeArea(Long.valueOf(request
					.getParameter("codearea")));
		} catch (Exception e) {
			subprogramaVO.setCodeArea(null);
		}

		subprogramaVO.setDescripcion(request.getParameter("descripcion"));

		subprogramaVO
				.setCodePrograma(Long.valueOf(request.getParameter("code")));

		subprogramaVO.setDiasReutilizar(request.getParameter("diasutil"));

		subprogramaVO.setValorUnitarioBeneficio(request.getParameter("valor"));

		subprogramaVO.setDesdeVigencia(request.getParameter("desde"));

		subprogramaVO.setHastaVigencia(request.getParameter("hasta"));

		subprogramaVO.setGrupoFamiliar(request.getParameter("grupo"));

		subprogramaVO.setValidarEstud(request.getParameter("estudiantes"));

		subprogramaVO.setValidarSisben(request.getParameter("sisben"));

		try {
			subprogramaVO.setVigencia(request.getParameter("vigencia"));
			if (subprogramaVO.getVigencia().equals("I")) {
				session.setAttribute("Codigo", subprog);
				return mapping.findForward("success2");
			}
		} catch (Exception e) {
			subprogramaVO.setVigencia("");
		}

		strvalor11 = request.getParameter("admon");

		try {
			subprogramaVO.setCodeUsuario(Long.valueOf(request
					.getParameter("admincodigo")));
		} catch (Exception e) {
			try {
				if (subprogramaVO.getCodeUsuario() == null) {
					subprogramaVO.setCodeUsuario(Long.valueOf(strvalor11));
				}
			} catch (Exception ex) {
				subprogramaVO.setCodeUsuario(null);
			}

		}

		subprogramaVO.setCampoA3(request.getParameter("diasrenovar"));
		subprogramaVO.setCampoA4(request.getParameter("paraEmpresa"));

		try {
			int modificar = subprogramaServiceImpl.update(subprogramaVO);

			if (modificar > 0) {
				session.setAttribute("mensajes",
						"El Beneficio fue modificado satisfactoriamente...");
			}
		} catch (DAOException e) {
			SUBException.logDebugError("", e);
			session.setAttribute("mensajes",
					"El beneficio no pudo ser modificado");
		}
		return mapping.findForward("success");
	}
}